package com.corundumstudio.socketio.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;

public class ChatLauncher {
	
	private static List<SocketIOClient> clients = new ArrayList<SocketIOClient>();//用于保存所有客户端  

    public static void main(String[] args) throws InterruptedException {

        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        final SocketIOServer server = new SocketIOServer(config);
        server.addEventListener("chatevent", ChatObject.class, new DataListener<ChatObject>() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
                // broadcast messages to all clients
                server.getBroadcastOperations().sendEvent("chatevent", data);
            }
        });
        
        server.addConnectListener(new ConnectListener() {
			
			@Override
			public void onConnect(SocketIOClient client) {
				clients.add(client);
			}
		});

        server.start();
        
        System.out.println("开始推送了..................");
        Timer timer = new Timer();  
        timer.schedule(new TimerTask() {  
            @Override  
            public void run() {  
                Random random = new Random();  
                for(SocketIOClient client : clients) {  
                    client.sendEvent("pushpoint", new Point(random.nextInt(100), random.nextInt(100),clients.size()));  
                }  
            }  
        }, 1000, 1000);  //每隔一秒推送一次
        
    }

}
